/*
 * Copyright 2005-2013 gaoyan.com. All rights reserved.
 * Support: http://www.gaoyan.com
 * License: http://www.gaoyan.com/license
 */
package com.sxhuayuan.parking.plugin.storage.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.sxhuayuan.parking.compenent.domain.Setting;
import com.sxhuayuan.parking.plugin.storage.FileInfo;
import com.sxhuayuan.parking.plugin.storage.StoragePlugin;
import com.sxhuayuan.parking.utils.SettingUtils;

/**
 * Plugin - 本地文件存储
 * 
 * @author gaoyan
 * @version 1.0
 */
@Component("filePlugin")
public class FilePlugin extends StoragePlugin implements ServletContextAware {

	/** servletContext */
	private ServletContext servletContext;

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@Override
	public String getName() {
		return "本地文件存储";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "gaoyan";
	}

	@Override
	public String getSiteUrl() {
		return "http://www.gaoyan.com";
	}

	@Override
	public String getInstallUrl() {
		return null;
	}

	@Override
	public String getUninstallUrl() {
		return null;
	}

	@Override
	public String getSettingUrl() {
		return "file/setting.jhtml";
	}

	@Override
	public String getCreatorCode(String path) {
		int endIndex = path.lastIndexOf(".");
		int beginIndex = path.substring(0, endIndex).lastIndexOf(".");
		if (beginIndex == -1) {
			return null;
		}
		String code = path.substring(beginIndex + 1, endIndex);
		return code;
	}

	// public static String getCreator_(String path) {
	// int endIndex = path.lastIndexOf(".");
	// int beginIndex = path.substring(0, endIndex).lastIndexOf(".");
	// if (beginIndex == -1) {
	// return null;
	// }
	// return path.substring(beginIndex + 1, endIndex);
	// }
	//
	// public static void main(String[] args) {
	// String name = "fasdf-fa-sdfasdfa-sf.jpg";
	// System.out.println(getCreator_(name));
	// }

	@Override
	public void upload(String path, File file, String contentType, FileInfo.Creator creator) {
		File destFile = new File(servletContext.getRealPath(path));
		try {
			FileUtils.moveFile(file, destFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getUrl(String path) {
		Setting setting = SettingUtils.get();
		return setting.getSiteUrl() + path;
	}

	@Override
	public List<FileInfo> browser(String path, FileInfo.Creator creator) {
		Setting setting = SettingUtils.get();
		List<FileInfo> fileInfos = new ArrayList<FileInfo>();
		File directory = new File(servletContext.getRealPath(path));
		if (directory.exists() && directory.isDirectory()) {
			for (File file : directory.listFiles()) {
				FileInfo fileInfo = new FileInfo();
				fileInfo.setName(file.getName());
				fileInfo.setUrl(setting.getSiteUrl() + path + file.getName());
				fileInfo.setIsDirectory(file.isDirectory());
				fileInfo.setSize(file.length());
				fileInfo.setLastModified(new Date(file.lastModified()));
				fileInfos.add(fileInfo);
			}
		}
		return fileInfos;
	}

	@Override
	public List<FileInfo> browser(String path, FileInfo.Creator creator, String marker, Integer size) {
		return null;
	}
}